//
//  MagicMoveBackTransition.m
//  CATransition
//
//  Created by xuxiwen on 16/6/11.
//  Copyright © 2016年 xuxiwen. All rights reserved.
//

#import "MagicMoveBackTransition.h"
#import "QYXDailyViewController.h"
#import "QYXDailyDetailViewController.h"
#import "QYXDailyTableViewCell.h"

@implementation MagicMoveBackTransition

#pragma mark - 返回动画时间

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.3f;
}

#pragma mark - 两个VC过渡动画


- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    
    // 目的VC
    QYXDailyViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    // 起始VC
    QYXDailyDetailViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    // 转场视图容器
    UIView *containerView = [transitionContext containerView];
    
    
  
    
    
    // 初始化第二个Vc
    
    toVC.view.frame = [transitionContext finalFrameForViewController:toVC];
    
    QYXDailyTableViewCell *cell = (QYXDailyTableViewCell *)[toVC.mainTableView cellForRowAtIndexPath:toVC.indexPath];
    cell.mainImageView.hidden = YES;
    
    [containerView insertSubview:toVC.view belowSubview:fromVC.view];
    [containerView addSubview:toVC.backView];
    
    
    // 发生动画
    
    
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        
        fromVC.view.alpha = 0;
        toVC.backView.frame = toVC.finiRect;
        
        
    } completion:^(BOOL finished) {
        
        [toVC.backView removeFromSuperview];
        cell.mainImageView.hidden = NO;
        fromVC.view.alpha = 1;

        
        // 结束
      [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
        
    }];
    
    
    
    
    
}
@end
